package org.wmine.visualization.ui;

import java.awt.Color;
import java.io.IOException;

import org.wmine.core.BasicTabPanel;
import org.wmine.core.IProjectManager;
import org.wmine.core.MainWindow;
import org.wmine.core.ProjectListener;
import org.wmine.core.ProjectManager;
import org.wmine.visualization.eventhandlers.ComboAlgoEventHandler;
import org.wmine.visualization.eventhandlers.DisplayGraphEventHandler;
import org.xml.sax.SAXException;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.border.Border;
import javax.xml.parsers.ParserConfigurationException;

/**
 * @author Rajeev Kumar Thakur
 *this panel is for visualization module
 */
public class VisualizationPanel extends BasicTabPanel implements ProjectListener {
	JPanel centralPanel;
	JPanel leftPanel;
	JComboBox comboMiningAlgo;
	JButton buttonDisplay;
	public String getTitle() {
		return "Visualization";
	}

	public String getTooltip() {
		return "Visualize the processed data in terms of Graphs";
	}

	public JPanel buildWindow() {
		//System.out.println("in visualise panel after build" + ProjectManager.getUniqueInstance().isProjectOpened());
		centralPanel = new JPanel();
		centralPanel.setLayout(null);
		centralPanel.setBounds(0,0,500,400);
		centralPanel.setVisible(true);
		//JLabel infoVisualise = new JLabel("Visualization Of Mining Algorithms");
		 
		
		//infoVisualise.setBounds(50,10,250,50);
		//centralPanel.add(infoVisualise);
	
			try {
				addLeftPanel();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		ProjectManager.getUniqueInstance().addProjectListener(this);
		
		return centralPanel;
	}

	private void addLeftPanel() throws ParserConfigurationException, SAXException, IOException {
		// TODO Auto-generated method stub
		leftPanel = new JPanel();
		leftPanel.setLayout(null);
		String temp = "<html>Select the Visualization Technique</html>";
		JLabel selectLabel = new JLabel(temp);
		selectLabel.setBounds(20, 10, 210,30);
		leftPanel.add(selectLabel);
		//JLabel labelSelectMiningAlgo = new JLabel("Mining Algorithms");
		//labelSelectMiningAlgo.setBounds(40,80,150,20);
		//leftPanel.add(labelSelectMiningAlgo);
		
		comboMiningAlgo = new JComboBox();
		addMiningAlgoList(comboMiningAlgo);
		comboMiningAlgo.setBounds(40,60,150,20);
		leftPanel.add(comboMiningAlgo);
		
		
					 
			
		buttonDisplay = new JButton("Display");
		buttonDisplay.setBounds(40,450,100,20);
		buttonDisplay.setEnabled(false);
		
		leftPanel.add(buttonDisplay);
		leftPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Input"));
		leftPanel.setBounds(10,10,250,560);
		centralPanel.add(leftPanel);
		
		DisplayGraphEventHandler displayButtonHandler = new DisplayGraphEventHandler(centralPanel);
		
		ComboAlgoEventHandler miningComboHandler = new ComboAlgoEventHandler(displayButtonHandler, leftPanel);
		//System.out.println("in left panel   "+ProjectManager.getUniqueInstance().isProjectOpened());
		comboMiningAlgo.addItemListener(miningComboHandler);
		buttonDisplay.addActionListener(displayButtonHandler);
		
		

	}
	/**
	 * @param comboBox
	 * this methods adds the types of mining algorithms for showing outputs in the combo box
	 * 
	 */
	public void addMiningAlgoList(JComboBox comboBox){
		//automate this hard coded method to read form index file and take 
		//the list of mining algorithms from index file		
		String subtypes[] = new String[20];
		IProjectManager ip = ProjectManager.getUniqueInstance();
			
		
		/*if(ip.isProjectOpened()){
			subtypes = ip.getSubtypesNamesArray("algorithm");
			System.out.println("in LeftPanel"+ subtypes[0].toString() ); 
			}*/
		comboBox.addItem("----Select----");
		if(ProjectManager.getUniqueInstance().isProjectOpened()){
			comboBox.addItem("Frequency Dist");
			comboBox.addItem("Linear Regression");
			comboBox.addItem("KMeans Clustering");
			comboBox.addItem("Scatter Plot");
			comboBox.addItem("Bar Chart");
			comboBox.addItem("Pie Chart");
			}	
		}

	public void onProjectEvent(int eventID) {
		//System.out.println("HIHI");
		if(eventID == ProjectManager.PROJECT_OPEN_EVENT) {
			buttonDisplay.setEnabled(true);
		}
		
		
	}
	
	 
}
